<?php
namespace app\model\book;

use think\db\Query;
use think\facade\Db;
use think\Model;
class OrderModel extends Model {

    protected $table = 'order';

    protected $field = '';

    public function list($where = [],$order = [],$limit = [0,3]) {
        $list = $this->where($where)->limit($limit[0],$limit[1])->select()->toArray();
        return $list;
    }

    /*
     * 四层查询, 使用模型效率极高
     * 订单列表 (order)
     * 每个订单有多个订单详情  (order_detail)
     * 每个详情对应一个商品   (book)
     * 每个商品对应一个分类   (book_cate)
     * */
    public function listFull($where = [],$order = [],$limit = [0,3]) {
        /* where in 方法查询效率高, 非关联查询 */

        $list = $this->with(['orderDetail'=>function(Query $query) {
            $query->with(['goodsDetail'=> function(Query $book) {
                $book->with('cateInfo')->field('id,title,cate_id');
            }])->field('id,order_id,book_id,num,unit_price');
        }])->where($where)->limit($limit[0],$limit[1])->select()->toArray();

        return $list;
    }

    public function orderDetail() {
        return $this->hasMany(OrderDetailModel::class,'order_id');
    }

}